package com.tenton.netty.unicast.custom;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName： UnicastCustomerHandler
 * @Description： 单播客户端 解析
 * @Author： tangdong
 * @Date： 2024/7/4 09:23
 */
@Slf4j
public class UnicastCustomerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket msg) throws Exception {
        ByteBuf byteBuf = msg.content();
        String json = byteBuf.toString(StandardCharsets.UTF_8);
        log.info("接收到信息：" + json);
        //按照协议格式解析
        //ByteBuf byteBuf = msg.content();
        //if (byteBuf.readableBytes() < 32) {
        //    log.error("数据有误，等待下一包！");
        //    byteBuf.resetWriterIndex();
        //}
        //int head = byteBuf.readIntLE();
        //if (head != 0xEB90EB91) {
        //    log.error("数据有误，等待下一包！");
        //    byteBuf.resetWriterIndex();
        //}
    }
}
